package org.minuframework.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description java
 * @author      minu
 * @date        2018/04/23 14:58:66
 * @deprecated
 */
public class TimeU {
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // constants
    
    public static final int SECOND = 1000;
    public static final int MINUTE = 60 * SECOND;
    public static final int HOUR   = 60 * MINUTE;
    public static final int DAY    = 24 * HOUR;
    public static final int WEEK   = 7 * DAY;
    
    // ------------------------------------------------------------------------
    
    public static final String DATETIME_FORMAT   = "yyyy-MM-dd HH:mm:ss";
    public static final String SIMPLEDATE_FORMAT = "yyyy-MM-dd";
    public static final String SIMPLETIME_FORMAT = "HH:mm:ss";
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // public methods
    
    // ------------------------------------------------------------------------
    
    /**
     * String转Date
     * @param  date   String
     * @param  format String
     * @return        日期 Date
     */
    public static Date formatDateTime(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        ParsePosition parp = new ParsePosition(0);
        return sdf.parse(date, parp);
    }
    
    /**
     * String转Date
     * @param  date String yyyy-MM-dd HH:mm:ss
     * @return      日期 Date
     */
    public static Date dateTimeFormat(String date) {
        return formatDateTime(date, DATETIME_FORMAT);
    }
    
    /**
     * String转Date
     * @param  date String yyyy-MM-dd
     * @return      日期 Date
     */
    public static Date simpleDateTime(String date) {
        return formatDateTime(date, SIMPLEDATE_FORMAT);
    }
    
    /**
     * String转Date
     * @param  date String HH:mm:ss
     * @return      日期 Date
     */
    public static Date simpleTimeFormat(String date) {
        return formatDateTime(date, SIMPLETIME_FORMAT);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * Date转String
     * @param  date   Date
     * @param  format String
     * @return        日期 String
     */
    public static String formatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    
    /**
     * Date转String
     * @param  format String
     * @return        日期 String
     */
    public static String formatDateTime(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
    
    /**
     * Date转String
     * @param  date Date
     * @return      日期 String yyyy-MM-dd HH:mm:ss
     */
    public static String dateTimeFormat(Date date) {
        return formatDateTime(date, DATETIME_FORMAT);
    }
    
    /**
     * Date转String
     * @param  date Date
     * @return      日期 String yyyy-MM-dd
     */
    public static String simpleDateTime(Date date) {
        return formatDateTime(date, SIMPLEDATE_FORMAT);
    }
    
    /**
     * Date转String
     * @param  date Date
     * @return      日期 String HH:mm:ss
     */
    public static String simpleTimeFormat(Date date) {
        return formatDateTime(date, SIMPLETIME_FORMAT);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 当前日期
     * @return 日期 String yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentDateTime() {
        return formatDateTime(new Date(), DATETIME_FORMAT);
    }
    
    /**
     * 当前日期
     * @return 日期 String yyyy-MM-dd
     */
    public static String getCurrentDateStamp() {
        return formatDateTime(new Date(), SIMPLEDATE_FORMAT);
    }
    
    /**
     * 当前日期
     * @return 日期 String HH:mm:ss
     */
    public static String getCurrentTimeStamp() {
        return formatDateTime(new Date(), SIMPLETIME_FORMAT);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * offset后的日期
     * @param  date   日期 Date
     * @param  offset 数量 int
     * @param  type   日期类型 Calendar
     * @return        日期 Date
     */
    public static Date offsetDateTime(Date date, int offset, int type) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(type, offset);
        return cal.getTime();
    }
    
    /**
     * offset年后的日期
     * @param  date   日期 Date
     * @param  offset 年数 int
     * @return        日期 Date
     */
    public static Date offsetDateYear(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.YEAR);
    }
    
    /**
     * offset月后的日期
     * @param  date   日期 Date
     * @param  offset 月数 int
     * @return        日期 Date
     */
    public static Date offsetDateMonth(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.MONTH);
    }
    
    /**
     * offset周后的日期
     * @param  date   日期 Date
     * @param  offset 周数 int
     * @return        日期 Date
     */
    public static Date offsetDateWeek(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.WEDNESDAY);
    }
    
    /**
     * offset天后的日期
     * @param  date   日期 Date
     * @param  offset 天数 int
     * @return        日期 Date
     */
    public static Date offsetDateDay(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.DAY_OF_YEAR);
    }
    
    /**
     * offset小时后的日期
     * @param  date   日期 Date
     * @param  offset 小时数 int
     * @return        日期 Date
     */
    public static Date offsetDateHour(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.HOUR);
    }
    
    /**
     * offset分钟后的日期
     * @param  date   日期 Date
     * @param  offset 分钟数 int
     * @return        日期 Date
     */
    public static Date offsetDateMinute(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.MINUTE);
    }
    
    /**
     * offset秒后的日期
     * @param  date   日期 Date
     * @param  offset 秒数 int
     * @return        日期 Date
     */
    public static Date offsetDateSecond(Date date, int offset) {
        return offsetDateTime(date, offset, Calendar.SECOND);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 昨日
     * @param  date 日期 Date
     * @return      昨日 Date
     */
    public static Date getYesterday(Date date) {
        return offsetDateDay(date, -1);
    }
    
    /**
     * 明日
     * @param  date 日期 Date
     * @return      明日
     */
    public static Date getTomorrow(Date date) {
        return offsetDateDay(date, 1);
    }
    
    /**
     * 返回日期
     * @param  date long型日期 long
     * @return      日期 Date
     */
    public static Date getDate(long date) {
        Date now = new Date();
        if (date <= 0) { return now; }
        now = new Date(date);
        return now;
    }
    
    // ------------------------------------------------------------------------
    
    public static long getTimeMillis(Date date) {
        long millis = -1;
        if (date == null) { return millis; }
        millis = date.getTime();
        return millis;
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 两个日期相隔月数
     * @param  date  small Date
     * @param  date2 big Date
     * @return       两个日期相隔月数 int
     */
    public static int differOfMonth(Date date, Date date2) {
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH);
        
        date2 = simpleDateTime(simpleDateTime(date2));
        cal.setTime(date2);
        int month2 = cal.get(Calendar.MONTH);
        
        return month2 - month;
    }
    
    /**
     * 两个日期相隔周数
     * @param  date  small Date
     * @param  date2 big Date
     * @return       两个日期相隔周数 int
     */
    public static int differOfWeek(Date date, Date date2) {
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        long time = cal.getTimeInMillis();
        
        date2 = simpleDateTime(simpleDateTime(date2));
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        
        long different = (time2 - time) / WEEK;
        return Integer.parseInt(String.valueOf(different));
    }
    
    /**
     * 两个日期相隔天数
     * @param  date  small Date
     * @param  date2 big Date
     * @return       两个日期相隔天数 int
     */
    public static int differOfDay(Date date, Date date2) {
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        long time = cal.getTimeInMillis();
        
        date2 = simpleDateTime(simpleDateTime(date2));
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        
        long different = (time2 - time) / DAY;
        return Integer.parseInt(String.valueOf(different));
    }
    
    /**
     * 两个日期相隔分小时数
     * @param  date  small Date
     * @param  date2 big Date
     * @return       两个日期相隔小时数 int
     */
    public static int differOfHour(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return Integer.MAX_VALUE; }
        
        long different = (date2.getTime() - date.getTime()) / HOUR;
        return Integer.parseInt(String.valueOf(different));
    }
    
    /**
     * 两个日期相隔分钟数
     * @param  date  small Date
     * @param  date2 big Date
     * @return       两个日期相隔分钟数 int
     */
    public static int differOfMinute(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return Integer.MAX_VALUE; }
        
        long different = (date2.getTime() - date.getTime()) / MINUTE;
        return Integer.parseInt(String.valueOf(different));
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 两个日期相隔月数
     * @param  date  small String
     * @param  date2 big String
     * @return       两个日期相隔月数 int
     */
    public static int differOfMonth(String date, String date2) {
        return differOfMonth(dateTimeFormat(date), dateTimeFormat(date2));
    }
    
    /**
     * 两个日期相隔周数
     * @param  date  small String
     * @param  date2 big String
     * @return       两个日期相隔周数 int
     */
    public static int differOfWeek(String date, String date2) {
        return differOfWeek(dateTimeFormat(date), dateTimeFormat(date2));
    }
    
    /**
     * 两个日期相隔天数
     * @param  date  small String
     * @param  date2 big String
     * @return       两个日期相隔天数 int
     */
    public static int differOfDay(String date, String date2) {
        return differOfDay(dateTimeFormat(date), dateTimeFormat(date2));
    }
    
    /**
     * 两个日期相隔分小时数
     * @param  date  small String
     * @param  date2 big String
     * @return       两个日期相隔小时数 int
     */
    public static int differOfHour(String date, String date2) {
        return differOfHour(dateTimeFormat(date), dateTimeFormat(date2));
    }
    
    /**
     * 两个日期相隔分钟数
     * @param  date  small String
     * @param  date2 big String
     * @return       两个日期相隔分钟数 int
     */
    public static int differOfMinute(String date, String date2) {
        return differOfMinute(dateTimeFormat(date), dateTimeFormat(date2));
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 周
     * @param  date 日期 Date
     * @return      周 int
     */
    public static int getWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return week <= 0 ? 7 : week;
    }
    
    /**
     * 周
     * @return 周 int
     */
    public static int getWeek() {
        return getWeek(new Date());
    }
    
    /**
     * 月份
     * @param  date 日期 Date
     * @return      月份 int
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }
    
    /**
     * 月份
     * @return 月份 int
     */
    public static int getMonth() {
        return getMonth(new Date());
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 是否为同年
     * @param  date  small String
     * @param  date2 big String
     * @return       是否为同年 boolean
     */
    public static boolean isSameYear(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return false; }
        
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        
        Calendar cal2 = Calendar.getInstance();
        date2 = simpleDateTime(simpleDateTime(date2));
        cal2.setTime(date2);
        
        return cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
    }
    
    /**
     * 是否为同月
     * @param  date  small String
     * @param  date2 big String
     * @return       是否为同月 boolean
     */
    public static boolean isSameMonth(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return false; }
        
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        
        Calendar cal2 = Calendar.getInstance();
        date2 = simpleDateTime(simpleDateTime(date2));
        cal2.setTime(date2);
        
        if (cal.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) { return false; }
        return cal.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
    }
    
    /**
     * 是否为同一周
     * @param  date  small String
     * @param  date2 big String
     * @return       是否为同一周 boolean
     */
    public static boolean isSameWeek(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return false; }
        
        Calendar cal = Calendar.getInstance();
        date = simpleDateTime(simpleDateTime(date));
        cal.setTime(date);
        
        Calendar cal2 = Calendar.getInstance();
        date2 = simpleDateTime(simpleDateTime(date2));
        cal2.setTime(date2);
        
        return cal.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR);
    }
    
    /**
     * 是否为同一天
     * @param  date  small String
     * @param  date2 big String
     * @return       是否为同一天 boolean
     */
    public static boolean isSameDay(Date date, Date date2) {
        if ((date == null) || (date2 == null)) { return false; }
        
        return simpleDateTime(date).equals(simpleDateTime(date2));
    }
    
    /**
     * 是否过期
     * @param  date      出厂日期 Date
     * @param  expireDay 保质期 int
     * @return           是否过期 boolean
     */
    public static boolean isExpire(Date date, int expireDay) {
        Date expire = offsetDateDay(date, expireDay);
        Date now = new Date();
        return now.after(expire);
    }
    
    /**
     * 将时长转换成"00:00:00.000"的字符串格式
     * @param  duration 时长（单位：秒）
     * @return          String
     */
    public static String timeFormatToString(long duration) {
        if (duration <= 0) { return "00:00:00.000"; }
        StringBuilder result = new StringBuilder();
        if (duration / HOUR > 0) {
            int hour = (int) (duration / HOUR);
            result.append(String.format("%02d", hour));
            duration -= hour * HOUR;
        } else {
            result.append("00");
        }
        if (duration / MINUTE > 0) {
            int minute = (int) (duration / MINUTE);
            result.append(":").append(String.format("%02d", minute));
            duration -= minute * MINUTE;
        } else {
            result.append(":00");
        }
        if (duration / SECOND > 0) {
            int second = (int) (duration / SECOND);
            result.append(":").append(String.format("%02d", second));
            duration -= second * SECOND;
        } else {
            result.append(":00");
        }
        if (duration > 0) {
            result.append(".").append(String.format("%03d", duration));
        } else {
            result.append(".000");
        }
        return result.toString();
    }
    
}
